package com.softWang.system.controller;

import com.softWang.common.response.ResponseResult;
import com.softWang.common.service.EmailService;
import com.softWang.common.service.SmsSendService;
import com.softWang.common.service.email.EmailSimpleRequestDTO;
import com.softWang.common.service.email.EmailTemplateRequestDTO;
import com.softWang.system.constants.EmailTemplateEnum;
import com.softWang.system.constants.SmsTemplateEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Api(value="测试",tags="测试")
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private SmsSendService smsSendService;
    @Resource
    private EmailService emailService;

    @PermitAll
    @ApiOperation(value="redisson分布式锁测试")
    @RequestMapping(value = "/testLock", method = RequestMethod.POST)
    public ResponseResult test(String threadFlag) {
        RLock testLock = redissonClient.getLock("testLock");
        try {
            System.out.println("test-s-"+threadFlag);
            if (testLock.tryLock(10, 10, TimeUnit.SECONDS)) {
                Thread.sleep(5000);     // 模拟业务处理时间
                System.out.println("test-"+threadFlag);
            } else {
                // 锁获取失败处理
                System.out.println("获取分布式锁失败-"+threadFlag);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();     // 处理异常
        } finally {     // 释放锁
            testLock.unlock();     // 释放锁
        }
        return ResponseResult.success("ok");
    }


    @PermitAll
    @ApiOperation(value="短信发送测试",notes = "{phone:手机号,code:验证码,content:{key1:v1,key2:v2}}")
    @RequestMapping(value = "/testSms", method = RequestMethod.POST)
    public ResponseResult test(@RequestBody LinkedHashMap<String, Object> params) throws Exception {
        String phone = (String) params.get("phone");
        String code = (String) params.get("code");
        SmsTemplateEnum codeEnum = SmsTemplateEnum.getEnum(code);
        LinkedHashMap<  String, Object> content = (LinkedHashMap<String, Object>) params.get("content");
        smsSendService.sendSms(phone, codeEnum.getTemplateCode(), content);
        return ResponseResult.success("ok");
    }

    @PermitAll
    @ApiOperation(value="邮件发送测试",notes = "{to:邮箱,templateCode:模板code,content:{key1:v1,key2:v2}}")
    @RequestMapping(value = "/testEmail", method = RequestMethod.POST)
    public ResponseResult testEmail(@RequestBody Map<String, Object> params) throws Exception {
        EmailTemplateEnum templateCode = EmailTemplateEnum.getEnum(params.get("templateCode").toString());
        EmailSimpleRequestDTO email = new EmailSimpleRequestDTO();
        email.setTo(Arrays.asList((String) params.get("to")));
        email.setSubject("测试邮件");
        email.setTemplateStr(templateCode.getTempalteContent());
        email.setParams((Map<String, Object>) params.get("content"));
        emailService.sendSimpleMail(email);
        return ResponseResult.success("ok");
    }

    @PermitAll
    @ApiOperation(value="邮件发送测试模板",notes = "{to:邮箱,templateName:模板名,content:{key1:v1,key2:v2}}")
    @RequestMapping(value = "/testEmail2", method = RequestMethod.POST)
    public ResponseResult testEmail2(@RequestBody Map<String, Object> params) throws Exception {
        EmailTemplateRequestDTO email = new EmailTemplateRequestDTO();
        email.setTo(Arrays.asList((String) params.get("to")));
        email.setSubject("测试邮件");
        email.setTemplateName((String) params.get("templateName"));
        email.setParams((Map<String, Object>) params.get("content"));
        emailService.sendTemplateMail(email);
        return ResponseResult.success("ok");
    }
}
